﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IBM.Data.DB2;

namespace VPBS.Common
{
    public class DB2Helper
    {
        public DB2Helper()
        {
        }

        public static DataSet ExecuteDataSet(string connectionString, CommandType commandType, string commandText)
        {
            DataSet dataSet = new DataSet("ResultDataSet");
            using (DB2Connection connection = new DB2Connection(connectionString))
            {
                DB2Exception exception;
                try
                {
                    connection.Open();
                }
                catch (DB2Exception exception2)
                {
                    exception = exception2;
                    throw new Exception("Open DB2 Connection", exception);
                }
                using (DB2Command command = new DB2Command(commandText, connection))
                {
                    command.CommandType = commandType;
                    using (DB2DataAdapter adapter = new DB2DataAdapter(command))
                    {
                        try
                        {
                            adapter.Fill(dataSet);
                        }
                        catch (Exception exception3)
                        {
                            throw new Exception("Fill From DB2", exception3);
                        }
                    }
                }
                try
                {
                    connection.Close();
                }
                catch (DB2Exception exception4)
                {
                    exception = exception4;
                    throw new Exception("Close DB2 Connection", exception);
                }
            }
            return dataSet;
        }

        public static DataSet ExecuteDataSet(string connectionString, CommandType commandType, string commandText, params DB2Parameter[] commandParameters)
        {
            DataSet dataSet = new DataSet("ResultDataSet");
            using (DB2Connection connection = new DB2Connection(connectionString))
            {
                DB2Exception exception;
                try
                {
                    connection.Open();
                }
                catch (DB2Exception exception2)
                {
                    exception = exception2;
                    throw new Exception("Open DB2 Connection", exception);
                }
                using (DB2Command command = new DB2Command(commandText, connection))
                {
                    command.CommandType = commandType;
                    foreach (DB2Parameter parameter in commandParameters)
                    {
                        command.Parameters.Add(parameter);
                    }
                    using (DB2DataAdapter adapter = new DB2DataAdapter(command))
                    {
                        try
                        {
                            adapter.Fill(dataSet);
                        }
                        catch (Exception exception3)
                        {
                            throw new Exception("Fill From DB2", exception3);
                        }
                    }
                }
                try
                {
                    connection.Close();
                }
                catch (DB2Exception exception4)
                {
                    exception = exception4;
                    throw new Exception("Close DB2 Connection", exception);
                }
            }
            return dataSet;
        }

        public static DataSet ExecuteDataSet(string connectionString, CommandType commandType, string commandText, params object[] commandParameterValues)
        {
            DataSet dataSet = new DataSet("ResultDataSet");
            using (DB2Connection connection = new DB2Connection(connectionString))
            {
                DB2Exception exception;
                try
                {
                    connection.Open();
                }
                catch (DB2Exception exception2)
                {
                    exception = exception2;
                    throw new Exception("Open DB2 Connection", exception);
                }
                using (DB2Command command = new DB2Command(commandText, connection))
                {
                    command.CommandType = commandType;
                    foreach (object obj2 in commandParameterValues)
                    {
                        command.Parameters.Add(obj2);
                    }
                    using (DB2DataAdapter adapter = new DB2DataAdapter(command))
                    {
                        try
                        {
                            adapter.Fill(dataSet);
                        }
                        catch (Exception exception3)
                        {
                            throw new Exception("Fill From DB2", exception3);
                        }
                    }
                }
                try
                {
                    connection.Close();
                }
                catch (DB2Exception exception4)
                {
                    exception = exception4;
                    throw new Exception("Close DB2 Connection", exception);
                }
            }
            return dataSet;
        }

        public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
        {
            int num = 0;
            using (DB2Connection connection = new DB2Connection(connectionString))
            {
                DB2Exception exception;
                try
                {
                    connection.Open();
                }
                catch (DB2Exception exception2)
                {
                    exception = exception2;
                    throw new Exception("Open DB2 Connection", exception);
                }
                using (DB2Command command = new DB2Command(commandText, connection))
                {
                    command.CommandType = commandType;
                    try
                    {
                        num = command.ExecuteNonQuery();
                    }
                    catch (Exception exception3)
                    {
                        throw new Exception("Execute query or store procedure From DB2", exception3);
                    }
                }
                try
                {
                    connection.Close();
                }
                catch (DB2Exception exception4)
                {
                    exception = exception4;
                    throw new Exception("Close DB2 Connection", exception);
                }
            }
            return num;
        }

        public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params DB2Parameter[] commandParameters)
        {
            int num = 0;
            using (DB2Connection connection = new DB2Connection(connectionString))
            {
                DB2Exception exception;
                try
                {
                    connection.Open();
                }
                catch (DB2Exception exception2)
                {
                    exception = exception2;
                    throw new Exception("Open DB2 Connection", exception);
                }
                using (DB2Command command = new DB2Command(commandText, connection))
                {
                    command.CommandType = commandType;
                    foreach (DB2Parameter parameter in commandParameters)
                    {
                        command.Parameters.Add(parameter);
                    }
                    try
                    {
                        num = command.ExecuteNonQuery();
                    }
                    catch (Exception exception3)
                    {
                        throw new Exception("Execute query or store procedure From DB2", exception3);
                    }
                }
                try
                {
                    connection.Close();
                }
                catch (DB2Exception exception4)
                {
                    exception = exception4;
                    throw new Exception("Close DB2 Connection", exception);
                }
            }
            return num;
        }

        public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params object[] commandParameterValues)
        {
            int num = 0;
            using (DB2Connection connection = new DB2Connection(connectionString))
            {
                DB2Exception exception;
                try
                {
                    connection.Open();
                }
                catch (DB2Exception exception2)
                {
                    exception = exception2;
                    throw new Exception("Open DB2 Connection", exception);
                }
                using (DB2Command command = new DB2Command(commandText, connection))
                {
                    command.CommandType = commandType;
                    foreach (object obj2 in commandParameterValues)
                    {
                        command.Parameters.Add(obj2);
                    }
                    try
                    {
                        num = command.ExecuteNonQuery();
                    }
                    catch (Exception exception3)
                    {
                        throw new Exception("Execute query or store procedure From DB2", exception3);
                    }
                }
                try
                {
                    connection.Close();
                }
                catch (DB2Exception exception4)
                {
                    exception = exception4;
                    throw new Exception("Close DB2 Connection", exception);
                }
            }
            return num;
        }

        public static DB2DataReader ExecuteReader(DB2Connection connection, CommandType commandType, string commandText)
        {
            DB2DataReader reader;
            try
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }
            }
            catch (DB2Exception exception)
            {
                throw new Exception("Open DB2 Connection", exception);
            }
            DB2Command command = new DB2Command(commandText, connection);
            command.CommandType = commandType;
            try
            {
                reader = command.ExecuteReader();
            }
            catch (Exception exception2)
            {
                throw new Exception("Execute reader From DB2", exception2);
            }
            return reader;
        }

        public static DB2DataReader ExecuteReader(DB2Connection connection, CommandType commandType, string commandText, params DB2Parameter[] commandParameters)
        {
            DB2DataReader reader;
            try
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }
            }
            catch (DB2Exception exception)
            {
                throw new Exception("Open DB2 Connection", exception);
            }
            DB2Command command = new DB2Command(commandText, connection);
            command.CommandType = commandType;
            foreach (DB2Parameter parameter in commandParameters)
            {
                command.Parameters.Add(parameter);
            }
            try
            {
                reader = command.ExecuteReader();
            }
            catch (Exception exception2)
            {
                throw new Exception("Execute reader From DB2", exception2);
            }
            return reader;
        }

        public static DB2DataReader ExecuteReader(DB2Connection connection, CommandType commandType, string commandText, params object[] commandParameterValues)
        {
            DB2DataReader reader;
            try
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }
            }
            catch (DB2Exception exception)
            {
                throw new Exception("Open DB2 Connection", exception);
            }
            DB2Command command = new DB2Command(commandText, connection);
            command.CommandType = commandType;
            foreach (object obj2 in commandParameterValues)
            {
                command.Parameters.Add(obj2);
            }
            try
            {
                reader = command.ExecuteReader();
            }
            catch (Exception exception2)
            {
                throw new Exception("Execute reader From DB2", exception2);
            }
            return reader;
        }

    }
}